<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * 处理museum表,目前只获取坐标
 */

class museumMakerController extends baseController{

    //获取坐标
    public function run(){
        $map = new mapController();
        $common = new commonController();
        $data = model('companies')->select('longitude="0" OR longitude=""');

        for($i = 0;isset($data[$i]);$i++) {
//            print_r($data[$i]);
            $name = '';
            $address = '';
            $city = '';
            $province = '';
            $location = array();
            $updata = array();
            if ($data[$i]) {
                if ($data[$i]['name'] != '') {
                    $name = $data[$i]['name'];
                }
                if ($data[$i]['address'] != '') {
                    $address = $data[$i]['address'];
                }
                if ($data[$i]['province'] != '') {
                    $province = $data[$i]['province'];
                }
                if ($data[$i]['city']) {
                    $city = $data[$i]['city'];
                } else {
                    if ($data[$i]['province'] != '') {
                        $city = $data[$i]['province'];
                    }
                }
                echo  $name,'>11<';
                $location = $map->getlocation($name, $city);
                if ($location == false || $location['longitude'] == '') {
                    echo $address,'>22<';
                    $location = $map->getlocation($address, $city);
                    if ($location == false|| $location['longitude'] == '') {
                        echo $province . $city . $address,'>33<';
                        $location = $map->getlocation($province . $city . $address, $city);
                    }
                }
                if ($location != false) {
                    $updata['longitude'] = $location['longitude'];
                    $updata['latitude'] = $location['latitude'];
                    if($data[$i]['city'] == '' && $location['city']!=''){
                        echo '城市为空且location有';
                        echo $data[$i]['id'],$data[$i]['name'];
                        print_r($location);
                        $cityid = $common->getcityid($location['city']);
                        $updata['city'] = $cityid['id'];
                    }
                    if($data[$i]['city'] == '暂无'&& $location['city']!=''){
                        echo '城市为暂无';
                        $cityid = $common->getcityid($location['city']);
                        $updata['city'] = $cityid['id'];
                    }
                    /*if($data[$i]['city'] == '上海市'&& $location['city']!=''){
                        echo '上海市';
                        $updata['city'] = $location['city'];
                    }*/
                    print_r($updata);
                    model('companies')->update('id="' . $data[$i]['id'] . '"', $updata);

                }

            }
        }
        return null;
    }

    //获取简介
    public function makedetailed(){

        $keyword = '';

        $data = model('companies')->select('about is NULL');
//        print_r($data);die;

        for($i = 0;isset($data[$i]);$i++){
            $keyword = $data[$i]['apellation'];
            $info = $this->getbaike($keyword);
            $something = json_decode($info,true);
//            print_r($something);die;
            $insert['about'] = $something['abstract'];
//            print_r($something['abstract']);die;
            model('companies')->update('id="'.$data[$i]['id'].'"',$insert);

        }
        return null;

    }

    protected function getbaike($keyword){
        $url = 'http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key='.$keyword.'&bk_length=600';
        return $this->curl($url,'');


    }




    private function curl($url, $data, $header = false, $method = "POST")
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $ret = curl_exec($ch);
        return $ret;
    }

}